#!/bin/bash

## Use mingw to build

#Change NDK to your Android NDK location
if [ "$NDK" = "" ]; then
    NDK=/d/Android/android-ndk-r10d
fi
if [ "$NDK_PLATFORM" = "" ]; then
    NDK_PLATFORM=$NDK/platforms/android-9/arch-arm
fi
if [ "$NDK_PREBUILT" = "" ]; then
    NDK_PREBUILT=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64
    NDK_PREBUILTLLVM=$NDK/toolchains/llvm-3.5/prebuilt/windows-x86_64
fi

set -e

GENERAL="\
   --enable-cross-compile \
   --enable-pic \
   --extra-libs="-latomic" \
   --arch=arm \
   --cc=$NDK_PREBUILTLLVM/bin/clang \
   --cross-prefix=$NDK_PREBUILT/bin/arm-linux-androideabi- \
   --ld=$NDK_PREBUILTLLVM/bin/clang \
   --nm=$NDK_PREBUILT/bin/arm-linux-androideabi-nm"

MODULES="\
   --disable-avdevice \
   --disable-filters \
   --disable-programs \
   --disable-avfilter \
   --disable-postproc \
   --disable-encoders \
   --disable-protocols \
   --disable-hwaccels \
   --disable-doc"

VIDEO_DECODERS="\
   --enable-decoder=h264" 

AUDIO_DECODERS=""
  
DEMUXERS=""

VIDEO_ENCODERS=""

AUDIO_ENCODERS=""

MUXERS=""


PARSERS=""


function build_ARMv6
{
./configure --target-os=linux \
    --prefix=./android/armv6 \
    ${GENERAL} \
    --sysroot=$NDK_PLATFORM \
    --extra-cflags=" --target=arm-linux-androideabi -O3 -DANDROID -fpic -fasm -fno-short-enums -fno-strict-aliasing -mfloat-abi=softfp -mfpu=vfp -marm -march=armv6" \
    --disable-shared \
    --enable-static \
    --extra-ldflags=" -B$NDK_PREBUILT/bin/arm-linux-androideabi- --target=arm-linux-androideabi -Wl,--rpath-link,$NDK_PLATFORM/usr/lib -L$NDK_PLATFORM/usr/lib -L$NDK_PREBUILT/arm-linux-androideabi/lib -nostdlib -lc -lm -ldl -llog" \
    --disable-zlib \
    --disable-xlib \
    --disable-bzlib \
	--disable-asm \
    --disable-everything \
    ${MODULES} \
    ${VIDEO_DECODERS} \
    ${AUDIO_DECODERS} \
    ${VIDEO_ENCODERS} \
    ${AUDIO_ENCODERS} \
    ${DEMUXERS} \
    ${MUXERS} \
    ${PARSERS} \
    --disable-neon

make clean
make -j4 install
}

function build_ARMv7
{
./configure --target-os=linux \
    --prefix=./android/armv7 \
    ${GENERAL} \
    --sysroot=$NDK_PLATFORM \
    --extra-cflags=" --target=arm-linux-androideabi -O3 -DANDROID -fpic -fasm -fno-short-enums -fno-strict-aliasing -mfloat-abi=softfp -mfpu=vfp -marm -march=armv7-a" \
    --disable-shared \
    --enable-static \
    --extra-ldflags=" -B$NDK_PREBUILT/bin/arm-linux-androideabi- --target=arm-linux-androideabi -Wl,--rpath-link,$NDK_PLATFORM/usr/lib -L$NDK_PLATFORM/usr/lib -L$NDK_PREBUILT/arm-linux-androideabi/lib -nostdlib -lc -lm -ldl -llog" \
    --disable-zlib \
    --disable-xlib \
    --disable-bzlib \
	--disable-asm \
	--disable-debug \
	--disable-symver \
	--disable-iconv \
	--enable-neon \
	--disable-avdevice --disable-swresample --disable-postproc --disable-avfilter --disable-network --disable-avformat --disable-pthreads \
    --disable-everything \
    --enable-runtime-cpudetect \
    ${MODULES} \
    ${VIDEO_DECODERS} \
    ${AUDIO_DECODERS} \
    ${VIDEO_ENCODERS} \
    ${AUDIO_ENCODERS} \
    ${DEMUXERS} \
    ${MUXERS} \
    ${PARSERS}

make clean
make -j4 install
}

mkdir -p android
export TMP=./android
export TMPDIR=./android

##build_ARMv6
build_ARMv7
echo Android ARM builds finished
